package com.bigmi.common.baidu.api;
 
import com.bigmi.common.baidu.auth.AccessToken;
import com.bigmi.common.baidu.auth.BaiduSensitiveConfig;
import com.alibaba.fastjson.JSON;
import com.bigmi.common.baidu.entity.ImageCheckReturn;
import com.bigmi.common.baidu.entity.ReturnStatusEnum;
import com.bigmi.common.baidu.entity.TextCheckReturn;
import com.bigmi.common.baidu.utils.Base64Util;
import com.bigmi.common.baidu.utils.FileUtil;
import com.bigmi.common.baidu.utils.HttpUtil;
import com.google.gson.Gson;
import org.springframework.stereotype.Component;

import java.io.File;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
 
/**
 * 内容审核
 */
@Component
public class CheckContent {
 
    /**
     * 文本审核
     *
     * @param text 需要审核的文本
     * @return
     */
    public  Map<String, Object> TextCensor(String text) {
        Map<String, Object> map = new TreeMap<String, Object>();
        //获取access_token
        String access_token = AccessToken.getAuth();
        try {
            //设置请求的编码
            String param = "text=" + URLEncoder.encode(text, "UTF-8");
            //调用文本审核接口并取得结果
            String result = HttpUtil.post(BaiduSensitiveConfig.CHECK_TEXT_URL, access_token, param);
 
            // JSON解析对象
            TextCheckReturn tcr = JSON.parseObject(result, TextCheckReturn.class);
            Integer conclusionType = tcr.getConclusionType();
            if (conclusionType != 1 && !conclusionType.equals("1")) {
                map.put("code", ReturnStatusEnum.FAILURE.getValue());
                map.put("log_id", tcr.getLog_id());
                map.put("conclusion", tcr.getConclusion());
                map.put("conclusionType", conclusionType);
                map.put("data", tcr.getData());
                return map;
            }
            map.put("code", ReturnStatusEnum.SUCCESS.getValue());
            map.put("log_id", tcr.getLog_id());
            map.put("conclusion", tcr.getConclusion());
            map.put("conclusionType", tcr.getConclusionType());
            return map;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
 
    /**
     * 图像审核
     *
     * @param // 需要审核的图片路径
     * @return
     */
    public  Map<String, Object> ImgCensor(byte[] imageList) {
        /*if (!new File(imagePath).exists()) {

        }*/
        if (imageList.length==0){
            throw new NullPointerException("图片不存在");
        }
        Map<String, Object> map = new TreeMap<String, Object>();
        //获取access_token
        String access_token = AccessToken.getAuth();
        try {
            //据文件路径读取byte[]数组
            //byte[] imgData = FileUtil.readFileByBytes(imagePath);
            //Base64格式编码
            //String imgStr = Base64Util.encode(imgData);
            String imgStr = Base64Util.encode(imageList);
            //URLEncoder格式化编码
            String param = "image=" + URLEncoder.encode(imgStr, "UTF-8");
            //调用图像审核接口
            String result = HttpUtil.post(BaiduSensitiveConfig.CHECK_IMAGE_URL, access_token, param);
 
            //JSON解析对象
            //阿里巴巴json解析无法使用空参构造方法选择其他
            // ImageCheckReturn icr = JSON.parseObject(result,ImageCheckReturn.class);
            Gson gson = new Gson();
            ImageCheckReturn icr = gson.fromJson(result,ImageCheckReturn.class);
            Integer conclusionType = icr.getConclusionType();
            if (conclusionType != 1 && !conclusionType.equals("1")) {
                map.put("code", ReturnStatusEnum.FAILURE.getValue());
                map.put("log_id", icr.getLog_id());
                map.put("conclusion", icr.getConclusion());
                map.put("conclusionType", icr.getConclusionType());
                map.put("data", icr.getData());
                return map;
            }
            map.put("code", ReturnStatusEnum.SUCCESS.getValue());
            map.put("log_id", icr.getLog_id());
            map.put("conclusion", icr.getConclusion());
            map.put("conclusionType", icr.getConclusionType());
            return map;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
 
}